<template>
  <div class="app-container">
    <div class="search-container">
      <el-form
        ref="queryFormRef"
        :model="queryParams"
        :inline="true"
        class="mt-3"
      >
        <el-form-item prop="mobile" label="手机号">
          <el-input
            v-model="queryParams.mobile"
            placeholder="手机号"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item prop="bizNo" label="渠道号">
          <el-input
            v-model="queryParams.bizNo"
            placeholder="渠道号"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item prop="id" label="订单号">
          <el-input
            v-model="queryParams.id"
            placeholder="订单号"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item prop="statu" label="状态">
          <el-input
            v-model="queryParams.statu"
            placeholder="状态"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="订购时间范围">
          <el-date-picker
            v-model="searchTime"
            type="datetimerange"
            unlink-panels
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            @change="handleChangeTime"
            :default-time="defaultTime"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="handleQuery"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="resetQuery"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="codeList"
        highlight-current-row
        border
        stripe
      >
        <el-table-column
          label="序号"
          type="index"
          width="60"
          :index="indexMethod"
        />
        <el-table-column label="手机号" prop="mobile" width="120" sortable />
        <el-table-column label="渠道号" prop="bizNo" width="120" sortable />
        <el-table-column label="订单号" prop="id" width="170" sortable />
        <el-table-column label="时间" prop="timeCreate" width="210" sortable />
        <el-table-column label="状态" prop="statu" width="100" sortable>
          <template #default="scope">
            <div :class="scope.row.statu === '0000' ? 'success-row' : ''">
              {{ `${scope.row.statu}` }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="状态描述"
          prop="errorMessage"
          min-width="220"
          sortable
        >
          <template #default="scope">
            <div
              :class="scope.row.errorMessage === '成功' ? 'success-row' : ''"
            >
              {{ `${scope.row.errorMessage}` }}
            </div>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.page"
        v-model:limit="queryParams.size"
        @pagination="handleQuery"
      />
    </el-card>
  </div>
</template>
<script setup lang="ts">
import { getOrderListPage } from "@/api/orderList";
import { OrderListQuery, OrderListPageVO } from "@/api/orderList/types";

defineOptions({
  name: "Role",
  inheritAttrs: false,
});

const queryFormRef = ref(ElForm);

const loading = ref(false);
const total = ref(0);
const defaultTime: [Date, Date] = [
  new Date(2000, 1, 1, 0, 0, 0),
  new Date(2000, 2, 1, 23, 59, 59),
]; // '12:00:00', '08:00:00'
const indexMethod = (index: number) => {
  return index + 1;
};
const searchTime = ref(null);
const queryParams = reactive<OrderListQuery>({
  page: 1,
  size: 10,
  queryType: -1,
});

const codeList = ref<OrderListPageVO[]>();

function initSearchTime() {
  const start = new Date(
    new Date(
      `${new Date().getFullYear()}-${new Date().getMonth() + 1}-${new Date().getDate()} 00:00:00`
    ).getTime() -
      3600 * 1000 * 24 * 31
  );
  const end = new Date(
    `${new Date().getFullYear()}-${new Date().getMonth() + 1}-${new Date().getDate()} 23:59:59`
  );
  searchTime.value = [start, end];
  console.log("🚀 ~ initSearchTime ~ searchTime:", searchTime);
  queryParams.dateStart = new Date(
    start.getTime() + 8 * 3600 * 1000
  ).toISOString();
  queryParams.dateEnd = new Date(end.getTime() + 8 * 3600 * 1000).toISOString();
}
/** 查询 */
function handleQuery() {
  loading.value = true;
  getOrderListPage(queryParams)
    .then((data) => {
      console.log("🚀 ~ .then ~ data:", data);
      codeList.value = data.rows;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}
/** 重置查询 */
function resetQuery() {
  queryFormRef.value.resetFields();
  queryParams.page = 1;
  initSearchTime();
  handleQuery();
}

function getOrderType(val: String) {
  let typeName = "";
  switch (val) {
    case "0000":
      typeName = "成功";
      break;
    case "5000":
      typeName = "获取验证码超时";
      break;
    case "6000":
      typeName = "验证码提交成功";
      break;
    case "7000":
      typeName = "验证码提交失败";
      break;
    case "8000":
      typeName = "程序异常";
      break;
    case "1000":
      typeName = "提交手机号";
      break;
    case "100X":
      typeName = "渠道参数校验失败";
      break;
    case "1005":
      typeName = "订单保存失败";
      break;
    case "999999":
      typeName = "过滤失败";
      break;
    case "2000":
      typeName = "过滤成功";
      break;
    case "3000":
      typeName = "发送成功";
      break;
    case "4000":
      typeName = "发送失败";
      break;
    default:
      typeName = val;
      break;
  }
  return typeName;
}
function handleChangeTime(val) {
  if (!val) {
    return;
  }
  console.log("🚀 ~ handleChangeTime ~ val:", val);
  const [start, end] = val;
  queryParams.dateStart = new Date(
    start.getTime() + 8 * 3600 * 1000
  ).toISOString();
  queryParams.dateEnd = new Date(end.getTime() + 8 * 3600 * 1000).toISOString();
}
// const tableRowClassName = ({ row, rowIndex }) => {
//   console.log("🚀 ~ tableRowClassName ~ row.statu:", row.statu);
//   if (row.statu === "0000") {
//     return "success-row";
//   }
//   return "";
// };
onMounted(() => {
  initSearchTime();
  handleQuery();
});
</script>
<style>
.success-row {
  color: green;
}
</style>
